<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#assign currentMenu="region"/>
    <#include "../common/header.ftl">
    <link rel="stylesheet" href="/js/plugins/treeView/bootstrap-treeview.min.css" type="text/css" />
    <script type="text/javascript" src="/js/plugins/treeView/bootstrap-treeview.min.js"></script>
    <script type="text/javascript">
        $(function () {

            //页面加载完之后就拿到tbody中的所有tr
            var tr ;
            tr = $("tbody tr:first").clone();

            //页面加载完毕发送ajax请求，拿到所有区域
            $.get("/region/listMenus.do",{type:"treeview"},function (rootMenus) {
                //treeView
                $('#treeview').treeview({
                    color: "#428bca",
                    showTags: true,
                    data: [{
                        text: "所有区域",
                        nodes: rootMenus
                    }],
                    //懒加载
                    lazyLoad: function (node) {
                        // console.log(node);
                        //node当前点击的节点
                        //发送ajax请求，查询该区域的字区域
                        $.get("/region/listMenus.do", {parentId: node.id,type:"treeview"}, function (childMenus) {
                            $('#treeview').treeview('addNode', [childMenus, node]);
                        })
                    },
                    onNodeSelected: function (event, data) {
                        // 事件代码...data就是该点击的节点的json对象
                        //这个方法就是给所有节点绑定事件的
                        //点击的时候，将值赋给表单中的tr
                        $.get("/region/listMenus.do", {parentId: data.id}, function (childMenus) {
                            //点击的时候将表格中原有的数据清空掉
                            $("tbody").html("");
                            //给表单解封
                            $("table").css("display", "");
                            //循环回来的子节点
                            $.each(childMenus, function (index, item) {
                                // tr[0].innerHTML(index+1);
                                // tr[1].innerHTML(item.name);
                                var row = tr.clone();
                                row.find("td")[0].innerHTML = index + 1;
                                row.find("td")[1].innerHTML = item.name;
                                //设置自定义属性，用户点击编辑的时候进行数据回显
                                var json = {id:item.id,name:item.name,state:item.state};
                                row.find("a").attr("data-json",JSON.stringify(json));
                                //赋好值之后就将这个东西拼在tbody后面
                                row.appendTo("tbody");
                            })
                        })
                    }
                })
            });
            //给添加按钮绑定点击事件
            $("#inputBtn").click(function () {
                show();
                var node = $('#treeview').treeview('getSelected')[0];
                if(node){
                    $("#editForm input[name='parent.name']").val(node.text);
                    $("#editForm input[name='parent.id']").val(node.id);
                }else{
                    $("#editForm input[name='parent.name']").val("所有区域");
                }
            })
            //给编辑按钮绑定事件
            $("tbody").on("click",".inputBtn", function() {
                //回显数据
                var json = $(this).data("json");
                //绑定了事件之后打开模态窗
                show(json);


            });
            //给保存按钮点击事件
            $(".submitBtn").click(function () {
                //提交表单
                $("#editForm").submit();
            })

        })
        //定义一个打开模态框的方法
        function show(json){
            //清楚所有的数据
            $("#inputModal").val("");
            if(json){
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='name']").val(json.name);
                $("#editForm select[name='state']").val(json.state);
                var node = $('#treeview').treeview('getSelected')[0];
                if(node){
                    $("#editForm input[name='parent.name']").val(node.text);
                    $("#editForm input[name='parent.id']").val(node.id);
                }else{
                    $("#editForm input[name='parent.name']").val("所有区域");
                }
            }
            //打开模态窗
            $("#inputModal").modal("show");
        }
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-lg-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-lg-9">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-head-line">旅游区域管理</h1>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-12">
                    <a href="javascript:" class="btn btn-success" id="inputBtn">
                        <span class="glyphicon glyphicon-plus"></span> 添加区域
                    </a>
                </div>
            </div>

            <div class="row">
                <div class="col-lg-4">
                    <div id="treeview"></div>
                </div>
                <div class="col-lg-8">
                    <table class="table table-striped table-hover" style="display: none">
                        <thead>
                            <tr>
                                <th>序号</th>
                                <th>区域</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>xx</td>
                                <td>xx</td>
                                <td>
                                    <a role="button" class="btn btn-info btn-xs inputBtn">
                                        <span class="glyphicon glyphicon-pencil"></span> 修改
                                    </a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="inputModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form id="editForm" class="form-horizontal" method="post" action="/region/saveOrUpdate.do">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">区域名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" placeholder="请输入区域名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">上级区域</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parent.name" readonly/>
                            <input type="hidden" class="form-control" name="parent.id" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">设为推荐</label>
                        <div class="col-sm-6">
                            <select name="state" class="form-control">
                                <option value="0">普通</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <a href="javascript:" class="btn btn-success submitBtn" aria-hidden="true">保存</a>
                <a href="javascript:" class="btn btn-info" data-dismiss="modal" aria-hidden="true">关闭</a>
            </div>
        </div>
    </div>
</div>
</body>
</html>